<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {

	public function index()
	{
			
	}
	
	function create_order_email($guid, $oto){
		$this->load->library('email');
		$this->email->from('deliri9@gmail.com', 'RusTmarket.com');
		$emails[]='KutsenkoAleksandr@gmail.com';
		//$emails[]=$oto['email'];
		//$emails[]='deliri9@gmail.com';
		$this->email->to($emails);
		//$this->email->to($emails);
		$this->email->subject('У Вас появился новый заказ');
		$this->email->message('Был зарегестрирован заказ, проверьте данные в прикрепленном файле и перейдите по следующей ссылке для подтверждения правильности http://rustmarket.com/check/order_check/'.$guid.'/1');
		$this->email->attach('orders/'.$guid.'.pdf');
		$this->email->send();
		echo $this->email->print_debugger();
	}
	function html($t)
	{
		header('Access-Control-Allow-Origin: *');
		
		$this->load->model('orders');
		$t=urldecode($t);
		
		$t=(json_decode($t,true));
		print_r($t);
		
		foreach($t['data'] as $servId=>$data2)
		{
			
			$i=0;
			foreach($data2 as $row)
			{
				$data[$i]['userName'] = $t['fio'];
				$data[$i]['phone'] = $t['phone'];
				$data[$i]['wishes'] = $t['whishes'];
				$data[$i]['otoId']=$t['agent'];
				$data[$i]['tourDayId']=$servId;
				$pdf['tourDayName'] = $this->orders->get_tour_name($data[$i]['tourDayId']);
					
				$data[$i]['orderDate'] = date('Y-m-d');
				$oto['email'] = $this->orders->get_oto_email($data[$i]['otoId']);
				$data[$i]['startDate']=$row['date'];
				$data[$i]['hotel']=$row['hotel'];
				$data[$i]['hotel']=str_replace('plus','+',$data[$i]['hotel']);
				$data[$i]['hotel']=str_replace('star','*',$data[$i]['hotel']);
				$data[$i]['hotel']=str_replace('slash','/',$data[$i]['hotel']);
				echo($data[$i]['hotel']);
				$idTable = $this->orders->get_listOfTables_id($data[$i]['tourDayId'], $data[$i]['hotel']);
				echo($idTable);
				$data[$i]['dinner']=$row['hb'];
				$data[$i]['child']=$row['child'];
				if ($data[$i]['dinner']){
					$dinnerPrice = $this->orders->get_price($idTable, $data[$i]['startDate'], 4);
					$tmp = $this->orders->get_price_N($data[$i]['otoId'], $data[$i]['startDate'], $data[$i]['tourDayId'], 4);
					$dinnerPriceN = $tmp['priceN'];
					$typeDinnerPriceN = $tmp['type'];
					$tmp = array();
					$pdf['dinner'] = '+';
				}
				else{
					$dinnerPrice = 0;
					$dinnerPriceN = 0;
					$pdf['dinner'] = '-';
				}
					
				if ($data[$i]['child']){
					$childPrice = $this->orders->get_price($idTable, $data[$i]['startDate'], 5);
					$tmp = $this->orders->get_price_N($data[$i]['otoId'], $data[$i]['startDate'], $data[$i]['tourDayId'], 5);
					$childPriceN = $tmp['priceN'];
					$typeChildPriceN = $tmp['type'];
					$tmp = array();
					$pdf['child'] = '+';
				}
				else{
					$childPrice = 0;
					$childPriceN = 0;
					$pdf['child'] = '-';
				}
					
				$data[$i]['priceType'] = $row['room_type'];
				switch($data[$i]['priceType'])
				{
					case 'SNGL':
						$data[$i]['priceType'] = 2;
						$pdf['priceType'] = 'SNGL';
						break;
					case 'DBL':
						$data[$i]['priceType'] = 1;
						$pdf['priceType'] = 'DBL';
						break;
					case 'TRPL':
						$data[$i]['priceType']=3;
						$pdf['priceType'] = 'TRPL';
						break;
				}
				$orderPrice = $this->orders->get_price($idTable, $data[$i]['startDate'], $data[$i]['priceType']);
				$tmp = $this->orders->get_price_N($data[$i]['otoId'], $data[$i]['startDate'], $data[$i]['tourDayId'], $data[$i]['priceType']);
				//exit (print_r($tmp));
				$orderPriceN = $tmp['priceN'];
				$this->log('orderPriceN tmp= '.$orderPriceN);
				$typeOrderPriceN = $tmp['type'];
				$this->log('typeOrderPriceN tmp= '.$typeOrderPriceN);
					
				$data[$i]['totalPrice'] = 0;
				$data[$i]['totalPriceN'] = 0;
					
				$data[$i]['guid'] = $this->guid();
				$this->log('guid= '.$data[$i]['guid']);
				$data[$i]['totalPrice'] = $orderPrice+$dinnerPrice+$childPrice;
				$this->log('total= '.$data[$i]['totalPrice']);
				if ($typeDinnerPriceN == 1){
					$pricetmp = $this->count_price_N($orderPrice, $dinnerPriceN);
					$dinnerPriceN = $pricetmp;
				}
				if ($typeChildPriceN == 1){
					$pricetmp = $this->count_price_N($orderPrice, $childPriceN);
					$childPriceN = $pricetmp;
				}
				if ($typeOrderPriceN == 1){
					$pricetmp = $this->count_price_N($orderPrice, $orderPriceN);
					$orderPriceN = $pricetmp;
				}
				$data[$i]['totalPriceN'] = $orderPrice+$orderPriceN+$dinnerPrice+$dinnerPriceN+$childPrice+$childPriceN;
				$this->log('totalN= '.$data[$i]['totalPriceN']);
				//exit (print_r($data[$i]));
					
				$pdf['orderId'] = $this->orders->save_order_tmp($data[$i]);
				$this->log('orderId= '.$pdf['orderId']);
				$this->create_order_pdf($data[$i], $pdf);
				$this->log('createdOrder');
				//exit (print_r($pdf));
				$this->create_order_email($data[$i]['guid'], $oto);
				$this->log('email sended');
			}
		}	
			
			
	/*	
		$idTable = $this->orders->get_listOfTables_id($data[$i]['tourDayId'], $data[$i]['hotel']);
		$data[$i]['dinner'] = (empty($_POST['dinner'])) ? 0 : $_POST['dinner'];
		$this->log('get dinner '.$data[$i]['dinner']);
		$this->log('get data complete');
		if ($data[$i]['dinner'] != 0){
			$dinnerPrice = $this->orders->get_price($idTable, $data[$i]['startDate'], 4);
			$tmp = $this->orders->get_price_N($data[$i]['otoId'], $data[$i]['startDate'], $data['tourDayId'], 4);
			$dinnerPriceN = $tmp['priceN'];
			$typeDinnerPriceN = $tmp['type'];
			$tmp = array();
			$pdf['dinner'] = '+';
		}
		else{
			$dinnerPrice = 0;
			$dinnerPriceN = 0;
			$pdf['dinner'] = '-';
		}
			
		$data['child'] = (empty($_POST['child'])) ? 0 : $_POST['child'];
		$this->log('get child '.$data['child']);
		if ($data['child'] != 0){
			$childPrice = $this->orders->get_price($idTable, $data['startDate'], 5);
			$tmp = $this->orders->get_price_N($data['otoId'], $data['startDate'], $data['tourDayId'], 5);
			$childPriceN = $tmp['priceN'];
			$typeChildPriceN = $tmp['type'];
			$tmp = array();
			$pdf['child'] = '+';
		}
		else{
			$childPrice = 0;
			$childPriceN = 0;
			$pdf['child'] = '-';
		}
			
		$data['priceType'] = $this->input->post('pricetype');
		$this->log('get price_type '.$data['priceType']);
		switch($data['priceType'])
		{
			case 1:
				$data['priceType'] = 2;
				$pdf['priceType'] = 'SNGL';
				break;
			case 2:
				$data['priceType'] = 1;
				$pdf['priceType'] = 'DBL';
				break;
			case 3:
				$pdf['priceType'] = 'TRPL';
				break;
		}
		$orderPrice = $this->orders->get_price($idTable, $data['startDate'], $data['priceType']);
		$this->log('order price = '.$orderPrice);
		$tmp = $this->orders->get_price_N($data['otoId'], $data['startDate'], $data['tourDayId'], $data['priceType']);
		$this->log('order price tmp= '.$tmp);
		//exit (print_r($tmp));
		$orderPriceN = $tmp['priceN'];
		$this->log('orderPriceN tmp= '.$orderPriceN);
		$typeOrderPriceN = $tmp['type'];
		$this->log('typeOrderPriceN tmp= '.$typeOrderPriceN);
			
		$data['totalPrice'] = 0;
		$data['totalPriceN'] = 0;
			
		$data['guid'] = $this->guid();
		$this->log('guid= '.$data['guid']);
		$data['totalPrice'] = $orderPrice+$dinnerPrice+$childPrice;
		$this->log('total= '.$data['totalPrice']);
		if ($typeDinnerPriceN == 1){
			$pricetmp = $this->count_price_N($orderPrice, $dinnerPriceN);
			$dinnerPriceN = $pricetmp;
		}
		if ($typeChildPriceN == 1){
			$pricetmp = $this->count_price_N($orderPrice, $childPriceN);
			$childPriceN = $pricetmp;
		}
		if ($typeOrderPriceN == 1){
			$pricetmp = $this->count_price_N($orderPrice, $orderPriceN);
			$orderPriceN = $pricetmp;
		}
		$data['totalPriceN'] = $orderPrice+$orderPriceN+$dinnerPrice+$dinnerPriceN+$childPrice+$childPriceN;
		$this->log('totalN= '.$data['totalPriceN']);
		//exit (print_r($data));
			
		$pdf['orderId'] = $this->orders->save_order_tmp($data);
		$this->log('orderId= '.$pdf['orderId']);
		$this->create_order_pdf($data, $pdf);
		$this->log('createdOrder');
		//exit (print_r($pdf));
		//$this->create_order_email($data['guid'], $oto);
		$this->log('email sended');*/
		
	}
	function create_order_pdf($data, $pd){
		require_once ('fpdf/fpdf.php');
		$pdf=new FPDF();
		$pdf->SetAuthor('RusTmarket');
		$pdf->SetTitle('Order');
		$pdf->SetFont('Times','B',20);
		$pdf->SetTextColor(50,60,100);
				
		$pdf->AddPage('P');
		$pdf->Image('images/logo.jpg',10,20,33,0,'jpg','http://www.rustmarket.com/');
		$pdf->SetXY(50,20);
		$pdf->SetDrawColor(50,60,100);
		$pdf->Cell(100,10,'Order -'.$pd['orderId'],1,0,'C',0);

		$pdf->SetXY (10,50);
		$pdf->SetDrawColor(50, 60, 100);
		$pdf->Rect(10, 50, 170, 155);
				
		$pdf->SetFontSize(14);
		$pdf->SetXY(15,55);
		$pdf->Cell(45,10,'user Name:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['userName']);
				
		$pdf->SetXY(15,70);
		$pdf->Cell(45,10,'Email:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['email']);
				
		$pdf->SetXY(15,85);
		$pdf->Cell(45,10,'Phone:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['phone']);
				
		$pdf->SetXY(15,100);
		$pdf->Cell(45,10,'Wishes:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['wishes']);
				
		$pdf->SetXY(15,115);
		$pdf->Cell(45,10,'Service:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['tourDayId']);
				
		$pdf->SetXY(15,130);
		$pdf->Cell(45,10,'Order date:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['orderDate']);
				
		$pdf->SetXY(15,145);
		$pdf->Cell(45,10,'Service date:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['startDate']);
				
		$pdf->SetXY(15,160);
		$pdf->Cell(45,10,'OtoId:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['otoId']);
		
		$pdf->SetXY(15,175);
		$pdf->Cell(45,10,'Price:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['totalPrice']);	
		
		$pdf->SetXY(15,190);
		$pdf->Cell(45,10,'Price N:',1,0,'L',0);
		$pdf->SetX(70);
		$pdf->Write(10,$data['totalPriceN']);

		$pdf->SetXY(30,260);
		$pdf->Write(10,date('d.m.Y'));
		$pdf->SetXY(80,260);
		$pdf->Write(10,$data['guid']);
		$pdf->Output('orders/'.$data['guid'].'.pdf','F');		
}
	function guid(){
	        mt_srand((double)microtime()*10000);
	        $charid = strtoupper(md5(uniqid(rand(), true)));
			$charid=str_replace('{','',$charid);
			$charid=str_replace('}','',$charid);
	        $hyphen = chr(45);
	        $uuid = substr($charid, 0, 8).$hyphen
	                .substr($charid, 8, 4).$hyphen
	                .substr($charid,12, 4).$hyphen
	                .substr($charid,16, 4).$hyphen
	                .substr($charid,20,12);
	        return $uuid;
	}
	function send(){
		$this->load->helper(array('form', 'url'));
		#$this->load->library('form_validation');
		$this->load->model('orders');
		#if ($this->form_validation->run() == FALSE)
		#{
			$data['userName'] = $this->input->post('user');
			$data['email'] = $this->input->post('email');
			$data['phone'] = $this->input->post('phone');
			$data['wishes'] = $this->input->post('wishes');
			
			
			$data['tourDayId'] = $this->input->post('tourdayid');
			$this->log('tourDayId '.$data['tourDayId']);
			$pdf['tourDayName'] = $this->orders->get_tour_name($data['tourDayId']);
			$this->log('tourDayName '.$pdf['tourDayName']);
			
			
			$data['orderDate'] = date('Y-m-d');
			$this->log('orderDate '.$data['orderDate'] );
			
			$data['startDate'] = $this->input->post('datestart');
			$this->log('startDate '.$data['startDate']);
			
			if(isset($_POST['otoId']))
			{
				$data['otoId'] = $this->input->post('otoId');
				$this->log('otoId '.$data['otoId']);
			}
			else
			{
				$otkuda=getenv("HTTP_REFERER");
				$this->log("Otkuda ".$otkuda);
				$www = strstr($otkuda,"ref=");
				$site=explode('/',$www);
				if(count($site)<2)
				{
					$site[2]='';
					$this->log(" Dont have site");
					exit();
				}
				else
				{
					$query="SELECT id FROM  `users` WHERE  `site` LIKE  '%".$site[2]."%'";
					$res=$this->db->query($query);
					if($res->num_rows()>0)
					{
						$data['otoId']=$res->row()->id;
					}
					else
					{
						$this->log($site." not in users");
						exit();
					}
				}
			}
			$oto['email'] = $this->orders->get_oto_email($data['otoId']);
			$this->log('emails oto '.$oto['email']);
			
			$data['prId'] = $this->input->post('client');
			$data['hotel'] = (empty($_POST['hotel'])) ? 3 : $_POST['hotel'];
			$this->log('id prin '.$data['prId']);
			$this->log('hotel '.$data['hotel']);
			
			$idTable = $this->orders->get_listOfTables_id($data['tourDayId'], $data['hotel']);
			$this->log('get getId table '.$idTable);
			$data['dinner'] = (empty($_POST['dinner'])) ? 0 : $_POST['dinner'];
			$this->log('get dinner '.$data['dinner']);
			$this->log('get data complete');
			if ($data['dinner'] != 0){
				$dinnerPrice = $this->orders->get_price($idTable, $data['startDate'], 4);
				$tmp = $this->orders->get_price_N($data['otoId'], $data['startDate'], $data['tourDayId'], 4);
				$dinnerPriceN = $tmp['priceN'];
				$typeDinnerPriceN = $tmp['type'];
				$tmp = array();
				$pdf['dinner'] = '+';
			}
			else{
				$dinnerPrice = 0;
				$dinnerPriceN = 0;
				$pdf['dinner'] = '-';
			}
			
			$data['child'] = (empty($_POST['child'])) ? 0 : $_POST['child'];
			$this->log('get child '.$data['child']);
			if ($data['child'] != 0){
				$childPrice = $this->orders->get_price($idTable, $data['startDate'], 5);
				$tmp = $this->orders->get_price_N($data['otoId'], $data['startDate'], $data['tourDayId'], 5);
				$childPriceN = $tmp['priceN'];
				$typeChildPriceN = $tmp['type'];
				$tmp = array();
				$pdf['child'] = '+';
			}
			else{
				$childPrice = 0;
				$childPriceN = 0;
				$pdf['child'] = '-';
			}
			
			$data['priceType'] = $this->input->post('pricetype');
			$this->log('get price_type '.$data['priceType']);
			switch($data['priceType'])
			{
				case 1:
					$data['priceType'] = 2;
					$pdf['priceType'] = 'SNGL';
					break;
				case 2:
					$data['priceType'] = 1;
					$pdf['priceType'] = 'DBL';
					break;
				case 3:
					$pdf['priceType'] = 'TRPL';
					break;
			}
			$orderPrice = $this->orders->get_price($idTable, $data['startDate'], $data['priceType']);
			$this->log('order price = '.$orderPrice);
			$tmp = $this->orders->get_price_N($data['otoId'], $data['startDate'], $data['tourDayId'], $data['priceType']);
			$this->log('order price tmp= '.$tmp);
			//exit (print_r($tmp));
			$orderPriceN = $tmp['priceN'];
			$this->log('orderPriceN tmp= '.$orderPriceN);
			$typeOrderPriceN = $tmp['type'];
			$this->log('typeOrderPriceN tmp= '.$typeOrderPriceN);
			
			$data['totalPrice'] = 0;
			$data['totalPriceN'] = 0;
			
			$data['guid'] = $this->guid();
			$this->log('guid= '.$data['guid']);
			$data['totalPrice'] = $orderPrice+$dinnerPrice+$childPrice;
			$this->log('total= '.$data['totalPrice']);
			if ($typeDinnerPriceN == 1){
				$pricetmp = $this->count_price_N($orderPrice, $dinnerPriceN);
				$dinnerPriceN = $pricetmp;
			}
			if ($typeChildPriceN == 1){
				$pricetmp = $this->count_price_N($orderPrice, $childPriceN);
				$childPriceN = $pricetmp;
			}			
			if ($typeOrderPriceN == 1){
				$pricetmp = $this->count_price_N($orderPrice, $orderPriceN);
				$orderPriceN = $pricetmp;
			}		
			$data['totalPriceN'] = $orderPrice+$orderPriceN+$dinnerPrice+$dinnerPriceN+$childPrice+$childPriceN;
			$this->log('totalN= '.$data['totalPriceN']);
			//exit (print_r($data));
			
			$pdf['orderId'] = $this->orders->save_order_tmp($data);
			$this->log('orderId= '.$pdf['orderId']);
			$this->create_order_pdf($data, $pdf);
			$this->log('createdOrder');
			//exit (print_r($pdf));
			$this->create_order_email($data['guid'], $oto);
			$this->log('email sended');
		#}
		//else
		//{
		//	$this->load->view('formsuccess');
		//}

		
	}
	function count_price_N($price, $proc){
		$tmp = $price/100*$proc;
		return $tmp;
	}

}